/*
 *  StateManager.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 9/21/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include <iostream>
#include "StateManager.h"

StateManager::StateManager() : curr_state(TITLE), player(new Player()), market(new Market()), universe(new Universe(market)),
                                curr_planet(universe->getSystems()[0]->getPlanets()[0]), 
                                titleScene(new TitleScene()), statsScene(new StatsScene(universe)), combatScene(new CombatScene()),planetScene(new PlanetScene()),
                                mapScene(new MapScene(universe)), marketScene(new MarketScene(market)), travelScene(new TravelScene(universe)),
                                commonsScene(new CommonsScene(market)), shipyardScene(new ShipyardScene()), distance(0), trader(true)
{
	//std::vector<std::string> itemList = market->getItemList();
	//std::cout << " " << std::endl;
	//for (int i=0; i < itemList.size(); i++) {
	//	std::cout << itemList[i] << std::endl;
	//	std::cout << market->getTradeGood(itemList[i])->getBasePrice() << std::endl;
	//}
}

state_t StateManager::getState()
{
	return curr_state;
}

void StateManager::setState(state_t nextState)
{
	curr_state = nextState;
}

state_t StateManager::render(SDL_Surface *screen)
{
    
    
	switch (curr_state) {
		case TITLE: curr_state = titleScene->draw(screen, player); break;
		case STATS:
            curr_state = (statsScene->draw(screen, player));
            setPlanet(universe->getSystems()[0]->getPlanets()[0]);
            break;
		case COMBAT: curr_state = (combatScene->draw(screen, player)); break;
		case MAP:
            travelScene->setA(curr_planet);
            curr_state = (mapScene->draw(screen, curr_planet, player->getFuel()));
            if (mapScene->getPlanet() != NULL){
                setPlanet(mapScene->getPlanet());
            }
			distance = mapScene->getDistance();
            travelScene->setB(curr_planet);
			
            break;
        case PLANET:
            curr_state = planetScene->draw(screen, curr_planet, player);
            if (curr_state == MARKET){
                trader = false;
            }
            break;
        case MARKET: curr_state = marketScene->draw(screen, curr_planet, player, trader); break;
        case TRAVEL: curr_state = travelScene->draw(screen, player, distance);
            distance -= travelScene->getDistanceMoved();
			if (curr_state == MARKET ) trader = true;
            break;
		case COMMONS: curr_state = commonsScene->draw(screen); break;
        case SHIPYARD: curr_state = shipyardScene->draw(screen, player); break;
        case SAVE:
            //intentionally empty, entirely handled by SpaceTrader.cpp
            break;
        case LOAD:
            //intentionally empty, entirely handled by SpaceTrader.cpp
            break;
		case LOADDB:
            //intentionally empty, entirely handled by SpaceTrader.cpp
            break;
		default:
			std::cerr << "invalid state" << std::endl;
			curr_state = QUIT;
            break;
	}
    
	return curr_state;
}

Planet* StateManager::getPlanet()
{
    return curr_planet;
}

void StateManager::setPlanet(Planet *p)
{
    curr_planet = p;
}


Player* StateManager::getPlayer()
{
	return player;
}
